<template>
  <div class="hero-container">
    <div class="search-form">
      <el-input
        v-model="herosName"
        placeholder="请输入搜索内容"
        style="width: 200px"
        class="filter-item filter-input"
        @keyup.enter.native="handleSearch"
      />
      <el-button
        class="filter-item filter-button"
        type="primary"
        icon="el-icon-search"
        @click="handleSearch"
      ></el-button>
    </div>
    <div class="table-content">
      <el-table
        v-loading="listLoading"
        :data="tableData"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
      >
        <el-table-column label="Hero Image" width="150" align="center">
          <template slot-scope="scope">
            <el-image
              style="width: 100px; height: 100px"
              lazy
              :src="scope.row.heroImage"
              :preview-src-list="scope.row.heroImageList"
            >
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="Hero Name" prop="heroName" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.heroName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="PRO Win Rate" prop="winRate" align="center">
          <template slot-scope="scope">
            {{ scope.row.winRate }}
          </template>
        </el-table-column>
        <el-table-column align="center" prop="pickCount" label="Pro Pick Count">
          <template slot-scope="scope">
            <span>{{ scope.row.pickCount }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" prop="banCount" label="Pro Ban Count">
          <template slot-scope="scope">
            <span>{{ scope.row.banCount }}</span>
          </template>
        </el-table-column>
      </el-table>
      <div class="table-pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="pageSize"
          background
          layout="total, prev, pager, next, jumper"
          :total="total"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { getHeroList } from "@/api/heros";
// import resData from "./response";
export default {
  data() {
    return {
      list: null,
      herosName: "",
      listLoading: false,
      tableData: [],
      pageSize: 5,
      //  pageSizes: [5, 10, 15, 20, 30, 40, 50], // select选项设置：条/页
      currentPage: 1,
      total: 0,
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = true
      getHeroList().then(response => {
        const heroList = response.map((item) => {
        return {
          ...item,
          heroName: item.localized_name,
          heroImage: `https://api.opendota.com${item.img}`,
          heroImageList: [`https://api.opendota.com${item.img}`],
          winRate: item.pro_win,
          pickCount: item.pro_pick,
          banCount: item.pro_ban,
        };
      });
      this.list = heroList;
      this.total = heroList.length;
      this.handelTableData();
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.handelTableData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.handelTableData();
    },
    handelTableData(search) {
      this.tableData = this.list
        .slice(
          (this.currentPage - 1) * this.pageSize,
          this.currentPage * this.pageSize
        )
        .filter(
          (data) =>
            !search ||
            data.heroName.toLowerCase().includes(search.toLowerCase())
        );
    },
    handleSearch() {
      const search = this.herosName;
      this.handelTableData(search);
    },
  },
};
</script>
<style lang="scss" scoped>
.hero-container {
  .search-form {
    margin-bottom: 10px;
    display: flex;
    justify-content: flex-end;
    .filter-item {
      margin-right: 10px;
    }
    .filter-button {
      margin-right: 0;
    }
  }
  .table-content {
    .table-pagination {
      margin-top: 15px;
    }
  }
}
</style>